/*
 * CargadorDeTextures.cpp
 *
 *  Created on: 02/12/2013
 *      Author: ezequiel
 */

#include "CargadorDeTextures.h"

CargadorDeTextures::CargadorDeTextures(unsigned int cantidadImagenes) {
	textureid = 0 ;
	this->cantidad = cantidadImagenes;
	current = 0 ;
	textures = new GLuint[cantidadImagenes];
	 glGenTextures(cantidadImagenes, textures);

}

CargadorDeTextures::~CargadorDeTextures() {
	delete this->textures;
}

int CargadorDeTextures::cargarImagen(const char* filename) {
	  // Load texture file
		    	int image_witdh;
		 	int image_height;
		 	int image_channels;
		 	unsigned char* image_buffer  = SOIL_load_image(filename, &image_witdh, &image_height, &image_channels, SOIL_LOAD_RGBA);

		     // Copy file to OpenGL
//		     glActiveTexture(GL_TEXTURE0);
		     glBindTexture(GL_TEXTURE_2D, this->textures[current]);
	//	     gluBuild2DMipmaps( GL_TEXTURE_2D, 3, image_witdh, image_height, GL_RGB, GL_UNSIGNED_BYTE, image_buffer );
		     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_witdh, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_buffer);
		     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

		     // Set the Tex1 sampler uniform to refer to texture unit 0
//		     int loc = glGetUniformLocation(this->handle, "Tex1");
//
//		     if( loc >= 0 )
//		     {
//		 		// We indicate that Uniform Variable sampler2D "text" uses  Texture Unit 0
//		         glUniform1i(loc, 0);
//		     }
//		     else
//		     {
//		         fprintf(stderr, "Uniform variable Tex1 not found!\n");
//		     }
		     unsigned int returnv  = this->textures[current];
		     current++;
		 return returnv;

}

int CargadorDeTextures::cargarImagenReflexion(const char* filename) {
	  // Load texture file
	int image_witdh;
	int image_height;
	int image_channels;
			 	unsigned char* image_buffer  = SOIL_load_image(filename, &image_witdh, &image_height, &image_channels, SOIL_LOAD_RGBA);
			 	 glBindTexture(GL_TEXTURE_2D, this->textures[current]);
			 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_witdh, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_buffer);
			 	 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
			 	 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
			 	 glEnable(GL_TEXTURE_GEN_S);
			 	  glEnable(GL_TEXTURE_GEN_T);

			     unsigned int returnv  = this->textures[current];
			     current++;
			 return returnv;

}
